
class Person : Comparator<Person>{
    var name:String = ""
        set(value) {
            field = value
            pinyin = PinyinUtils.toPinyin(value,PinyinType.WORD_FIRST_UPPERCASE)
        }

    var pinyin:String = ""
        set(value) {
            field = value
            title = pinyin.first().uppercaseChar()
            nameAbbr = pinyin.filter { it in 'A'..'Z' }
        }

    var title:Char = '0'

    var nameAbbr:String = "" //名字首字母缩写

    override fun toString(): String {
        return "$name $pinyin"
    }

    //自己制定比较的方式 比较复杂 或者封装
    override fun compare(o1: Person?, o2: Person?): Int {
        if (o1 == null && o2 == null) return 0
        if (o1 != null && o2 == null) return 1
        if (o1 == null && o2 != null) return -1
        //先比较缩略字母
        if (o1!!.nameAbbr != o2!!.nameAbbr)
            return o1.nameAbbr.compareTo(o2.nameAbbr)
        //缩略名相同了 直接比全名
        return o1.pinyin.compareTo(o2.pinyin)
    }

}